/************************************************************************
 * The code is owned by mainframer.cn and Shang Tao
 * Without aproval of mainframer.cn and Shang Tao, removing the copyright
 * infomation is regarded as invassion of proprietary copyright
 ***********************************************************************/
package com.mr.qa.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;

import org.finj.FTPClient;
import org.finj.FTPException;

import com.mr.qa.GlobalConfigs;

/**
 * this class is responsible for sending a file to ftp server
 */

public class FtpUtil
{

    public FtpUtil()
    {
    }
    /**
     * add a file to ftp server
     * @param in
     * @return 0 successful, 1 no specified ftp server, 2 ftp can not be connected
     * 3 fail without specific causes
     */
    public static int uploadFile(String fileName,InputStream in)
    {
        if(checkFTPServerConfig() != 0)return checkFTPServerConfig();
        
        FTPClient ftpClient = null;
        try
        {
            //1.connect to server
            ftpClient = new FTPClient(GlobalConfigs.ftp_server,
            new Integer(GlobalConfigs.ftp_server_port),
            GlobalConfigs.ftp_server_user_name,GlobalConfigs.ftp_server_user_password.toCharArray());
            //2.set to current working directory 
             System.out.println("ftp_server_file_location="+GlobalConfigs.ftp_server_file_location);
            ftpClient.setWorkingDirectory(GlobalConfigs.ftp_server_file_location);
            
            //3.upload
            String path = GlobalConfigs.ftp_server_file_location+File.separator+fileName.getBytes();
            System.out.println("filename="+path);
            ftpClient.appendToFile(in,path);
            //
        }
        catch(IOException ftpe)
        {
            return 3;
        }
        catch(FTPException ftpe)
        {
            return 2;
        }
        finally
        {
            try
            {
                if(ftpClient != null)ftpClient.close();
            }catch(Exception e){}
        }
        return 0;
    }
    
    public static int removeFile(String fileName)
    {
        int checkResult = checkFTPServerConfig();
        if(checkResult != 0)return checkResult;
        FTPClient ftpClient = null;
        try
        {
            //1.connect to server
            ftpClient = new FTPClient(GlobalConfigs.ftp_server,
            new Integer(GlobalConfigs.ftp_server_port),
            GlobalConfigs.ftp_server_user_name,GlobalConfigs.ftp_server_user_password.toCharArray());
            //2.set to current working directory 
             System.out.println("ftp_server_file_location="+GlobalConfigs.ftp_server_file_location);
            ftpClient.setWorkingDirectory(GlobalConfigs.ftp_server_file_location);
            
            //3.delete
            String path = GlobalConfigs.ftp_server_file_location+File.separator+fileName.getBytes();
            ftpClient.deleteFile(path);
            //
        }
        catch(IOException ftpe)
        {
            return 3;
        }
        catch(FTPException ftpe)
        {
            return 2;
        }
        finally
        {
            try
            {
                if(ftpClient != null)ftpClient.close();
            }catch(Exception e){}
        }
        return 0;
    }
    
    private static int checkFTPServerConfig()
    {
        if(GlobalConfigs.ftp_server == null || 
        GlobalConfigs.ftp_server.length() == 0)return 1;
        if(GlobalConfigs.ftp_server_port == null || 
        GlobalConfigs.ftp_server_port.length() == 0)return 1;
        if(GlobalConfigs.ftp_server_file_location == null || 
        GlobalConfigs.ftp_server_file_location.length() == 0)return 1;
        return 0;
    }
    
    /**
     * retrieve a string 
     * @return
     */
    private static String getCurrentDateString()
    {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        String monthStr = ""+month;
        if(month < 10)monthStr = "0"+month;
        return year+"-"+monthStr;
    }
    
    static public void main(String str[])
    {
        try
        {
            FtpUtil ftpUtil = new FtpUtil();
            System.out.println("1");
            InputStream in = ftpUtil.getClass().getClassLoader().getResourceAsStream("log4j.properties");
            System.out.println("2");
            FtpUtil.uploadFile("log4j.properties",in);
            System.out.println("3");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}
